from google.appengine.api import urlfetch
import logging, urllib
from django.utils import simplejson
import dlz_config as dc
from ft_table import FtTable
from upload import *


USERNAME = 'ymeng@dlz.com'
PASSWORD = 'dlzlab'

class IformTable():
    def __init__(self, username, password, page_id, table_name):
        if username is None:
            username = USERNAME
        if password is None:
            password = PASSWORD
        self.page_id = page_id
        self.table_name = table_name
        self.username = username
        self.password = password
        self.data = None
    def query(self, since_id):
        logging.info('query iformtable, since_id=%s' % since_id)
        return download_table(self.username, self.password, self.page_id, self.table_name, record_id=None, since_id = since_id )
    
    def all(self):
        return download_table(self.username, self.password, self.page_id, self.table_name, record_id=None )
        
    def get(self, ID):
        return download_table(self.username, self.password, self.page_id, self.table_name, record_id=ID )
    

    
def download_table(username, password,page_id, table_name, record_id=None, since_id=None, qtype=None ):
    '''download a table and return a json'''
    form_fields = {
      "USERNAME": username,
      "PASSWORD": password,
      'PAGE_ID': page_id,
      'TABLE_NAME': table_name,
      'TYPE': 'TOP'
    }
    if since_id:
        form_fields['SINCE_ID'] = since_id
    if record_id:
        form_fields['ID'] = record_id
    if qtype:
        form_fields['TYPE'] = qtype
        
    url = "https://www.iformbuilder.com/exzact/dataJSON.php"
    logging.info('Downloading data from iFormBuilder: %s, %s' % (page_id, table_name))
    form_data = urllib.urlencode(form_fields)
    result = urlfetch.fetch(url=url,
                    payload=form_data,
                    method=urlfetch.POST)
    data = simplejson.loads(result.content)
    logging.info('data downloaded: %s' % str(data)[:100])
    return data


def process_iform_json(data):
    '''
    process the json returned from iformbuilder server
    format the data into record list with lat, lon
    '''
    results = []
    for item in data:
        result = {}
        record = item.get('record')
        location = item.get('location')
        for k, v in record.items():
            result[k] = v
        if location:
            result['lat'] = location.get('lat')
            result['lon'] = location.get('lon')
        results.append(result)
    return results


def process_compaction_form(data):
    '''
    Need to add the project_name, project_number, client_name
    '''
    tableid = int(dc.get('DLZ_PROJECT_LIST')['ft_table_id'])
    f = FtTable(None, None, tableid)
    project_list = f.all()
    
    def get_project(project_key):
        for p in project_list:
            project_number = p['project_number']
            if project_key.startswith(project_number):
                return p
     
    for record in data:
        project_key = record['project_key']
        p = get_project(project_key)
        if p:
            for fld in 'project_name project_number client_name'.split():
                record[fld] = p[fld]
    return data

def sync_compaction_form(since_id=None):
    ts = dc.get('DLZ_COMPACTION_FORM')
    logging.info('since id is : %s' % since_id)
    compaction_form = IformTable(None, None, ts['iform_page_id'], ts['iform_table_name'])
    if since_id is not None:
        pass
    else:
        f = FtTable(None, None, ts['ft_table_id'])
        sql = "select ID from %s order by ID DESC LIMIT 1" % ts['ft_table_id']
        result = f.SqlQuery(sql)
        since_id = result[0]['ID']
    data = compaction_form.query(since_id)
    if data:
        data = process_iform_json(data)
        data = process_compaction_form(data)
        insert_compaction_form(data)
    
def sync_compaction_test(since_id=None):
    ts = dc.get('DLZ_COMPACTION_TEST')
    compaction_test = IformTable(None, None, ts['iform_page_id'], ts['iform_table_name'])
    if since_id is not None:
        pass
    else:
        f = FtTable(None, None, ts['ft_table_id'])
        sql = "select ID from %s order by ID DESC LIMIT 1" % ts['ft_table_id']
        result = f.SqlQuery(sql)
        since_id = result[0]['ID']
    data = compaction_test.query(since_id)
    if data:
        data = process_iform_json(data)
        insert_compaction_test(data)